m4 (computer language)

m4
Paradigm(s) macro preprocessor
Appeared in 1977
Designed by Brian Kernighan, Dennis Ritchie.
Major implementations GNU m4

m4 is a general purpose macro processor designed by Brian Kernighan and Dennis Ritchie. m4 is an extension of an earlier macro processor m3, written by Ritchie for the AP-3 minicomputer.[1]

Contents

Use

All UNIXes make the m4 macro processor available, and POSIX has standardized it. Most users require m4 simply as a dependency of GNU autoconf, used by "configure" scripts, although the language can be, and is, used stand-alone.

A macro processor (or a preprocessor) operates as a text-replacement tool. End-users often employ it to re-use text templates, typically in programming applications, but also in text editing and in text-processing applications.

History

Macro processors became popular when programmers commonly used assembly language. In those early days of programming, programmers noted that much of their programs consisted of repeated text, and they invented simple means for reusing this text. Programmers soon discovered the advantages not only of reusing entire blocks of text, but also of substituting different values for similar parameters. This defined the usage range of macro processors.

Kernighan and Ritchie developed m4 in 1977, basing it on the ideas of Christopher Strachey. The distinguishing features of this style of macro preprocessing included:

The implementation of Rational Fortran used m4 as its macro engine from the beginning; and most Unix variants ship with it.

As of 2009 many applications continue to use m4 as part of the GNU Project's autoconf. It also appears in the configuration process of sendmail (a widespread mail transfer agent) and for generating footprints in the gEDA toolsuite.

m4 has many uses in code generation, but (as with any macro processor) problems can be hard to debug.[2]

Features

m4 offers these facilities:

Unlike most earlier macro processors, m4 does not target any particular computer or human language; historically, however, its development originated for supporting the Ratfor dialect of Fortran. Unlike some other macro processors, m4 is Turing-complete as well as a practical programming language.

Example

The following fragment gives a simple example that could form part of a library for generating HTML code. It defines a commented macro to number sections automatically:

divert(-1)
This `divert' discards this text. Note that I had to quote the `divert'
in the comment so it wouldn't get undiverted.

# In a true comment, I'm free to use words such as divert and other builtin
# m4 macros' names without consequence.

# This starts the count at ONE as the incr is a preincrement.
define(`H2_COUNT', 0)

# The H2_COUNT macro is redefined every time the H2 macro is used.
define(`H2',
	`define(`H2_COUNT', incr(H2_COUNT))<h2>H2_COUNT. $1</h2>')

divert(0)dnl Diversion to 0 means back to normal. dnl macro removes this line.
H2(First Section)
H2(Second Section)
H2(Conclusion)

Processing this code with m4 should generate the following text:

<h2>1. First Section</h2>
<h2>2. Second Section</h2>
<h2>3. Conclusion</h2>

Note the frequent use of dnl, which deletes up to and including the newline, thus preventing many blank lines appearing in the output.

Free software implementations

A GNU version of m4 exists.[3][4] FreeBSD, NetBSD, and OpenBSD also provide independent implementations of the m4 language. Furthermore, the Heirloom Project Development Tools includes a free version of the m4 language, derived from OpenSolaris.

See also

References

  1. ^ Brian W. Kernighan and Dennis M. Ritchie. The M4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. pdf
  2. ^ Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. pdf
  3. ^ GNU m4 web site "GNU M4", accessed 7 Oct 2010.
  4. ^ GNU m4 manual, online and for download in HTML, PDF, and other forms. "GNU M4 — GNU macro processor", accessed 7 Oct 2010.

External links